package net.zjitc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/9/28.
 */
@WebServlet(name = "TimeServlet",urlPatterns = "/Time")
public class TimeServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String lastTime=null;
        Cookie[] cookies=request.getCookies();
        for (int i=0;cookies != null && i<cookies.length;i++){
            if("lastAccessTime".equals(cookies[i].getName())){
                lastTime=cookies[i].getValue();
                break;
            }
        }
        if (lastTime==null){
            response.getWriter().print("首次访问");
        }else {
            response.getWriter().print("上次访问时间"+lastTime);}

        String currentTime=new SimpleDateFormat("yyyy-MM-dd#HH:mm:ss").format(new Date());
        Cookie cookie=new Cookie("lastAccessTime",currentTime);
        response.addCookie(cookie);
    }
}
